local gPeen = CreateFrame"MessageFrame"

local _G = getfenv(0)
local bit_bor = _G.bit.bor
local COMBATLOG_FILTER_ME = COMBATLOG_FILTER_ME

local hit, crit = 0, 0
local lastHit, lastCrit = 0, 0

local font = "Interface\\Addons\\pMinimap\\media\\font.ttf"
local size = 12

LibStub:GetLibrary("RCE"):Embed(gPeen)
gPeenDB = {}

gPeen:SetTimeVisible(2)
gPeen:SetInsertMode"TOP"
gPeen:SetFrameStrata"HIGH"
gPeen:SetHeight(60)
gPeen:SetWidth(512)
gPeen:SetPoint("TOP", UIParent, "TOP", 0, -300)
gPeen:SetFontObject(ErrorFont)
gPeen:SetFont("Fonts\\FRIZQT__.TTF", 26, "OUTLINE")
gPeen:Show()

function gPeen:BuffPeen(spellName, amount, critical)
	assert(type(spellName) == "string", format("Bad argument #1 to gPeen:BuffPeen (string expected, got %s)", type(spellName)))
	assert(type(amount) == "number", format("Bad argument #2 to gPeen:BuffPeen (number expected, got %s)", type(amount)))

	if not _G.gPeenDB[spellName] then
		_G.gPeenDB[spellName] = {
			normal = 0,
			critical = 0,
		}
	end

	if critical then
		if amount > _G.gPeenDB[spellName].critical then
			_G.gPeenDB[spellName].critical = amount
			self:AddMessage(spellName.." : "..amount.." (CRIT)", 1, 1, 1, 1, 2)
		end
	else
		if amount > _G.gPeenDB[spellName].normal then
			_G.gPeenDB[spellName].normal = amount
			self:AddMessage(spellName.." : "..amount, 1, 1, 1, 1, 2)
		end
	end
	
	for spell in pairs(_G.gPeenDB) do
		if _G.gPeenDB[spell].normal > lastHit then
			hit = _G.gPeenDB[spell].normal
			lastHit = hit
		end
		
		if _G.gPeenDB[spell].critical > lastCrit then
			crit = _G.gPeenDB[spell].critical
			lastCrit = crit
		end
	end
	
	text:SetText("|cffffffff"..hit.."|r / |cffffffff"..crit.."|r")
end

local function Damage(self, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellID, spellName, spellSchool, amount, school, resisted, blocked, absorbed, critical)
	if bit_bor(srcFlags, COMBATLOG_FILTER_ME) == COMBATLOG_FILTER_ME then
		if not resisted and not blocked and not absorbed then
			self:BuffPeen(spellName, amount, critical)
		end
	end			
end

local function Heal(self, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, amount, critical)
	if bit_bor(srcFlags, COMBATLOG_FILTER_ME) == COMBATLOG_FILTER_ME then
		self:AddHeal(spellName, amount, critical)
	end
end

gPeen:RegisterCombatEvents("SPELL_DAMAGE", "SPELL_PERIODIC_DAMAGE", Damage)
gPeen:RegisterCombatEvents("SPELL_HEAL", "SPELL_PERIODIC_HEAL", Heal)

local text = gPeen:CreateFontString(nil, "OVERLAY")

gPeen:SetScript("OnEvent", function()
	text:SetParent(UIParent)
	text:SetFont(font, size)
	text:ClearAllPoints()
	text:SetPoint("RIGHT", _G["gDPS"], "LEFT", -20, 0)
	text:SetTextColor(0, .8, 1)

	for spell in pairs(_G.gPeenDB) do
		if _G.gPeenDB[spell].normal > lastHit then
			hit = _G.gPeenDB[spell].normal
			lastHit = hit
		end
		
		if _G.gPeenDB[spell].critical > lastCrit then
			crit = _G.gPeenDB[spell].critical
			lastCrit = crit
		end
	end
	
	text:SetText("|cffffffff"..hit.."|r/|cffffffff"..crit.."|r")
	text:Show()
	
	local button = CreateFrame("Button", "sucks", UIParent)
	button:SetWidth(text:GetStringWidth())
	button:SetHeight(12)
	button:SetPoint("RIGHT", _G["gDPS"], "LEFT", -20, 0)
	button:SetScript("OnEnter", function()
		GameTooltip:SetOwner(button, "ANCHOR_NONE")
		GameTooltip:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", -225, 25)
		GameTooltip:AddDoubleLine("Spell Name", "Normal / Critical", 0, .8, 1, 0, .8, 1)
		for spell in pairs(_G.gPeenDB) do
			GameTooltip:AddDoubleLine(spell, _G.gPeenDB[spell].normal.." / ".._G.gPeenDB[spell].critical, 1, 1, 1, 1, 1, 1)
		end
		GameTooltip:Show()
	end)
	button:SetScript("OnLeave", function() GameTooltip:Hide() end)
end)

gPeen:RegisterEvent("PLAYER_ENTERING_WORLD")


